package com.bgs.controller;

import com.bgs.pojo.examStorage;
import com.bgs.pojo.studentInformation;
import com.bgs.pojo.vo.studentInformationVO;
import com.bgs.service.StudenService;
import com.bgs.service.examStorageService;
import com.bgs.utils.MyContent;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {

    Logger log = LogManager.getLogger();


    @Autowired
    private StudenService studenService;

    @Autowired
    private examStorageService exService;

    @RequestMapping("/we")
    public String we(){
        return "welcome1";
    }


    @RequestMapping("/li")
    public String lo(){
        return "stulist";
    }

    @RequestMapping("/li8")
    public String li8(){
        return "stu8";
    }

    @RequestMapping("/lis")
    public String lis(){
        return "stu8s";
    }

    @RequestMapping("/splist")
    public String splist(){
        return "splist";
    }


//    //全查1
//    @RequestMapping("/StudentfindAll")
//    public String StudentfindAll(Model model){
//        List<studentInformation> stu = studenService.findAll();
//
//        model.addAttribute("stu",stu);
//
//        return "stulist1";
//    }

    //全查2-json
    @RequestMapping("/StudentfindAll1")
    @ResponseBody
    public  Map<String,Object> StudentfindAll1(){

        Map<String,Object> m = new HashMap<>();


        String sJson ="";

        List<studentInformation> stu = studenService.findAllqc();
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map

        log.info("List<studentInformation>:::"+stu);

        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,stu.size());
        m.put(MyContent.DATA,stu);


        return m;
    }

    //模糊差-json
    @RequestMapping("/StudentfindAll")
    @ResponseBody
    public  Map<String,Object> StudentfindAll(@RequestBody studentInformationVO studentInformationVO){

        //模糊查询前端传过来的数据
        log.info("模糊查询前端传过来的数据studentInformationVO"+studentInformationVO);

        Map<String,Object> m = new HashMap<>();

        studentInformation studentInformation = new studentInformation();

        //将VO的属性一个一个set给stu
        BeanUtils.copyProperties(studentInformationVO,studentInformation);
        log.info("VO传给的studentInformation"+studentInformation);

        String sJson ="";

        List<studentInformation> stu = studenService.findAll(studentInformation);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map

        log.info("List<studentInformation>:::"+stu);

        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,stu.size());
        m.put(MyContent.DATA,stu);


        return m;
    }



    //考试

   /* @RequestMapping("/findAllandmohusp")
    @ResponseBody
    public Map<String,Object> findAllandmohusp(@RequestBody ClassroomVO cVo){
        log.info("模糊查询的条件"+cVo);
        Map<String,Object> m = new HashMap<>();
        //设置分页信息(当前页码，每页显示数量)
        PageHelper.startPage(cVo.getPage(),cVo.getLimit());
        //调用分页查询账单类列表的方法
        List<Classroom> CS = cS.findAllandmohu(cVo);
        //创建分页对象
        PageInfo<Classroom> pageInfo = new PageInfo<Classroom>(CS);
        // JSONObject jsonObj = JSONObject.parseObject(sJson);
        // m = jsonObj; json对象转成map
        log.info("List<Classroom>:::"+pageInfo);
        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,pageInfo.getTotal());
        m.put(MyContent.DATA,pageInfo.getList());

        return m;
    }*/



    //全查2-json
    @RequestMapping("/findAllsp")
    @ResponseBody
    public  Map<String,Object> findAllsp(){

        Map<String,Object> m = new HashMap<>();

        List<examStorage> stu = exService.findAllqc();

        log.info("List<examStorage>:::"+stu);

        m.put(MyContent.CODE,0);
        m.put(MyContent.MSG,"查询成功！");
        m.put(MyContent.COUNT,stu.size());
        m.put(MyContent.DATA,stu);


        return m;
    }

    /**
     * 删除商品
     */
    @RequestMapping("/deleteByspId")
    @ResponseBody
    public Map<String, Object> deleteByspId(@RequestBody Integer pkId){
        Map<String,Object> m = new HashMap<>();
        if(exService.deleteById(pkId)>0){
            m.put("success",true);
            m.put("message","删除成功");
        }else{
            m.put("success",false);
            m.put("message","删除失败");
        }
        return m;
    }


    @RequestMapping("/addClass")
    @ResponseBody
    public Map<String,Object> addClass (@RequestBody examStorage c){
        HashMap<String, Object>  m = new HashMap<>();
        log.info("添加的"+c);
        if (exService.insertSelective(c) > 0){

            m.put("success",true);
            m.put("message","添加成功");
        }else{
            m.put("success",false);
            m.put("message","添加失败");
        }
        return m;
    }





}
